#version 320 es
precision highp float;

layout(location = 0) uniform highp sampler2D tex_input;
layout(location = 1) uniform highp usampler2D utex_input;
layout(location = 2) uniform highp isampler2D itex_input;

layout(location = 5) uniform highp sampler2D tex_kernel;    //纹理卷积核

layout(location = 20) uniform lowp ivec2 kernelshape;   //卷积核的尺寸
layout(location = 21) uniform lowp int iszeropadding;   //超出输入纹理边界时，是否填充为0
layout(location = 22) uniform lowp ivec2 iotype;        //type.x输入的纹理数据类型 type.y输出的纹理数据类型
layout(location = 23) uniform mediump ivec2 texsize;    //输入纹理的宽高

layout(location = 0) out highp vec4 FragColor;
layout(location = 1) out highp uvec4 uFragColor;
layout(location = 2) out highp ivec4 iFragColor;

void main()
{
    FragColor = vec4(0);
    for(lowp int i=0;i<kernelshape.y;i++){
        for(lowp int j=0;j<kernelshape.x;j++){
            vec4 value;
            mediump ivec2 pos = ivec2(gl_FragCoord.xy);
            pos.x = pos.x -((kernelshape.x+1)/2 - 1);
            pos.y = pos.y -((kernelshape.y+1)/2 - 1);
            pos.x = pos.x + j;
            pos.y = pos.y + i;
            if(iotype.x == 0) value = texelFetch(tex_input,pos,0);
            else if(iotype.x == 1) value = vec4(texelFetch(utex_input,pos,0));
            else if(iotype.x == 2) value = vec4(texelFetch(itex_input,pos,0));
            if(iszeropadding!=0){   //超出边界清零
                value = value * float(all(greaterThanEqual(pos,ivec2(0)) && lessThan(pos,texsize)));
            }
            value = value * texelFetch(tex_kernel,ivec2(j,i),0);
            FragColor = FragColor + value;
        }
    }
    uFragColor = uvec4(FragColor);
    iFragColor = ivec4(FragColor);
}
